
const requsts = require("/util/requst");
const util = require("/util/util");
Page({
  data: {
    ishidden:true,
    windowHeight:"",
    userIds:"",
    listArr:[],
    userNo:"",
  },
  // onSearchBarTap() {
  //   my.navigateTo({
  //     url: '/page/common/search/search',
  //   });
  // },
  ditaddress(e){
    my.navigateTo({
       url: '/page/common/editaddress/editaddress?id='+e.currentTarget.dataset.id,
    });
  },
  submit(){
    my.navigateTo({
       url: '/page/common/addaddress/addaddress',
    });
  },
  //长按显示默认and删除
  longTapfn(e){
    var arrss=this.data.listArr;
    this.setData({
      userNo:e.currentTarget.dataset.userNo
    })
    e.currentTarget.dataset.index
      arrss[e.currentTarget.dataset.index].ishidden=!arrss[e.currentTarget.dataset.index].ishidden
      this.setData({
        listArr:arrss
      })
  },
  hidefn(e){
      var arrss=this.data.listArr;
      arrss[e.currentTarget.dataset.index].ishidden=!arrss[e.currentTarget.dataset.index].ishidden
      this.setData({
        listArr:arrss
      })
  },
  setdele(){
      var param={
      userId:this.data.userIds,
      shippingId:this.data.userNo,
      type:2,
    }
    console.log(param)
    var that=this
      my.request({
        url: requsts.url+"/user/operatorShippingAddress",
        method: 'POST',
        headers: {'content-type': 'application/x-www-form-urlencoded'},
        data:param,
        dataType: 'json',
        success: function(res) {
          console.log(res)
          if(res.data.code!=0){
              util.toastfn(res.data.msg)
          }else{
              that.init()
          }
        },
        fail: function(res) {
            util.toastfn(res.errorMessage)
        },
      });
  },
  onLoad(){
    // my.setStorage({
    //   key: 'userId',
    //   data: {
    //     userId: '2088522144147466',
    //   },
    //   success: function() {
        
    //   }
    // });
      this.init()
  },
  init(){
      var that=this
    my.getStorage({
      key: 'userId',
      success: function(res) {
        //my.alert({content: '获取成功：' + res.data.userId});
        //return
        that.setData({
          userIds:res.data.userId
        })
        var param={
          userId:that.data.userIds
        }
        my.request({
          url: requsts.url+"/user/getUserShippingAddressList",
          method: 'POST',
          headers: {'content-type': 'application/x-www-form-urlencoded'},
          data:param,
          dataType: 'json',
          success: function(res) {
            console.log(res)
            if(res.data.code){
                util.toastfn(res.data.msg)
            }
            var dataArr=res.data.data
            for(var i = 0;i<dataArr.length;i++){
              dataArr[i].ishidden=true
            }
            that.setData({
              listArr:dataArr
            })
            console.log(that.data.listArr)
          },
          fail: function(res) {
              util.toastfn(res.errorMessage)
          },
        });
      },
      fail: function(res){
        util.toastfn(res.errorMessage)
      }
    });
  },
  //设置为默认地址
  setmoren(){
    var param={
      userId:this.data.userIds,
      shippingId:this.data.userNo,
      type:3,
      isDefault:1
    }
    console.log(param)
    var that=this
      my.request({
        url: requsts.url+"/user/operatorShippingAddress",
        method: 'POST',
        headers: {'content-type': 'application/x-www-form-urlencoded'},
        data:param,
        dataType: 'json',
        success: function(res) {
          console.log(res)
          if(res.data.code!=0){
              util.toastfn(res.data.msg)
          }else{
              that.init()
          }
        },
        fail: function(res) {
            util.toastfn(res.errorMessage)
        },
      });
  },
  addbodyfn(){
    var arrss=this.data.listArr;
      for(var i = 0;i<arrss.length;i++){
              arrss[i].ishidden=true
            }
      this.setData({
        listArr:arrss
      })
  },
});